define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {
    function get_need_number(){
        $.post("qingdongwms/customer/need/get_need_number",function (data){
            top.window.Backend.api.sidebar({
                'qingdongwms/customer/need/index':[data.data.number, 'red', 'badge'],
                'qingdongwms/customer/need':[data.data.number, 'red', 'badge'],
                'qingdongwms/examine/examine/index':[data.data.contract, 'red', 'badge'], //合同
                'qingdongwms/examine/examine/consume':[data.data.consume, 'red', 'badge'],
                'qingdongwms/examine/examine/receivables':[data.data.receivables, 'red', 'badge'],
                'qingdongwms/examine/examine/achievement':[data.data.achievement, 'red', 'badge'],
                'qingdongwms/examine/examine/work':[data.data.approval, 'red', 'badge'],
                'qingdongwms/canlendar/canlendar':[data.data.eventOne, 'red', 'badge'],
            });
        },'json');
    }
    get_need_number();
    setInterval(function (){
        get_need_number();
    },50000);
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'qingdongwms/canlendar/canlendar/index?need='+Fast.api.query("need"),
                    add_url: 'qingdongwms/canlendar/canlendar/add',
                    edit_url: 'qingdongwms/canlendar/canlendar/edit',
                    del_url: 'qingdongwms/canlendar/canlendar/del',
                    table: 'canlendar'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'title', title: __('标题'), operate:"like", formatter: function (value, row) {
                                if(row.relation_type == 'event'){
                                    return "<a href='javascript:void(0);' data-id='" + row.id + "' class='show-event-detail'>" + value + "</a>";
                                }else{
                                    return "";
                                }

                            }},
                        {field: 'event_type', title: __('类型'), operate:"like"},
                        {field: 'start_time', title: __('开始时间'), operate:false},
                        {field: 'end_time', title: __('结束时间'), operate:false},
                        {field: 'staff.name', title: __('归属人'), operate:false},
                        {field: 'status', title: __('状态'),formatter:Table.api.formatter.status,
                            searchList: {'0':'未开始', 1:'执行中',2:'已结束',3:'已取消',}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                    ]
                ],
                //启用普通表单搜索
                commonSearch: true,
                searchFormVisible: true,
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $('.search').hide();
            $('.btn-import').hide();

            $(document).on('click', '.show-event-detail', function () {
                var id = $(this).data('id');
                var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                };
                Fast.api.open("qingdongwms/event/event/detail?ids=" + id, '任务详情', options);
            });
        },
        add: function () {
            $('.relation_type').change(function(){
                var type = $(this).val();
                var url = '';
                if(type == 1){
                    url = 'qingdongwms/canlendar/canlendar/customer';
                }else if(type == 2){
                    url = 'qingdongwms/canlendar/canlendar/contacts';
                }else if(type == 3){
                    url = 'qingdongwms/canlendar/canlendar/contract';
                }else{
                    url = 'qingdongwms/canlendar/canlendar/leads';
                }
                $.ajax({url:url,success:function(ret) {
                        if (ret.code == 1) {
                            var data = ret.data;
                            var length = data.length;
                            var html = '';
                            for (var i = 0; i < length; i++) {
                                html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                            }
                            $('#c-type').html(html);
                            $('select').selectpicker('refresh');
                        }
                    }
                });

            });
            Controller.api.bindevent();

        },

        edit: function () {
            $('.relation_type').change(function(){
                var type = $(this).val();
                var url = '';
                if(type == 1){
                    url = 'qingdongwms/canlendar/canlendar/customer';
                }else if(type == 2){
                    url = 'qingdongwms/canlendar/canlendar/contacts';
                }else if(type == 3){
                    url = 'qingdongwms/canlendar/canlendar/contract';
                }else{
                    url = 'qingdongwms/canlendar/canlendar/leads';
                }
                $.ajax({url:url,success:function(ret) {
                        if (ret.code == 1) {
                            var data = ret.data;
                            var length = data.length;
                            var html = '';
                            for (var i = 0; i < length; i++) {
                                html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                            }
                            $('#c-type').html(html);
                            $('select').selectpicker('refresh');
                        }
                    }
                });

            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    var style = row.storage == 'upyun' ? '!/fwfh/120x90' : '';
                    return '<img src="' + row.image + '" data-tips-image alt="" title="封面图" style="max-height:90px;max-width:120px">';

                },

            }
        }

    };
    return Controller;
});